<?php
/**
 * Description of SubQuestionclass
 *
 * @author Chisheng Lu, Jeffy Shih
 * @copyright Copyrigh(c)2010 DoRISE team, Science Education center, NTNU.
 * @since 2010-03-09
 * @version 1.0
 */
class SubQuestion {
    //put your code here
    private $mysqli;

    function __construct() {
        //引入資料庫連結參數設定
        include '../inc/oaConn.inc.php';
        $this->mysqli = @new mysqli($mysqlHost,$mysqlUser,$mysqlPassword,$mysqlDB);
        if(mysqli_connect_errno()) {
            throw new Exception('無法建立資料庫連線！'.mysqli_connect_error());
            $this->mysqli = false;
            exit();
        }

        //連線使用編碼
        $this->mysqli->query("SET NAMES 'utf8'");
    }

    // $arySubQuestion: Array
    // Returns Boolean
    public function addSubQuestion($arySubQuestion) {
        $intRtn = 0;
        $strNow = $this->getNowTime();
        $sql_addSQ = 'INSERT INTO `SubQuestion` (';
        $sql_values = ' VALUES(';
        if (array_key_exists('TId', $arySubQuestion)) {
            $sql_addSQ .= 'TId, ';
            $sql_values .= $arySubQuestion['TId'] . ', ';
        }
        if (array_key_exists('SQType', $arySubQuestion)) {
            $sql_addSQ .= 'SQType, ';
            $sql_values .= $arySubQuestion['SQType'] . ', ';
        }
        if (array_key_exists('SQContent', $arySubQuestion)) {
            $sql_addSQ .= 'SQContent, ';
            $sql_values .= '\'' .$arySubQuestion['SQContent'] . '\', ';
        }
        if (array_key_exists('MQId', $arySubQuestion)) {
            $sql_addSQ .= 'MQId, ';
            $sql_values .= $arySubQuestion['MQId'] . ', ';
        }
        if (array_key_exists('UserId', $arySubQuestion)) {
            $sql_addSQ .= 'UserId, ';
            $sql_values .= $arySubQuestion['UserId'] . ', ';
        }
        $sql_addSQ .= 'SQCreated';
        $sql_values .= '\'' . $strNow . '\') ';
        $sql_addSQ .= ')' . $sql_values . ';';
        $rs_addSQ = $this->mysqli->query($sql_addSQ);
        if (!$rs_addSQ) {
            throw new Exception('新增子題資料時發生錯誤！');
        }else {
            $intRtn = $this->mysqli->insert_id;
        }
        return $intRtn;

    }

    // $intSQId: int
    // $arySubQuestion: Array
    // Returns Boolean
    public function editSubQuestion($intSQId, $arySubQuestion) {
        //檢查子題編號是否為空白
        if (empty ($intSQId)) {
            throw new Exception('缺少子題編號！');
        }
        $bolRtn = false;
        $strNow = $this->getNowTime();
        $sql_editSQ = 'Update `SubQuestion` SET ';
        if (array_key_exists('TId', $arySubQuestion))
            $sql_editSQ .= '`TId` = ' . $arySubQuestion['TId'] . ', ';
        if (array_key_exists('SQType', $arySubQuestion))
            $sql_editSQ .= '`SQType` = ' . $arySubQuestion['SQType'] . ', ';
        if (array_key_exists('SQContent', $arySubQuestion))
            $sql_editSQ .= '`SQContent` = \'' . $arySubQuestion['SQContent'] . '\', ';
        if (array_key_exists('UserId', $arySubQuestion))
            $sql_editSQ .= '`SQModifyUserId` = ' . $arySubQuestion['UserId'] . ', ';
        $sql_editSQ .= '`SQModified` = \'' . $strNow . '\' ';
        $sql_editSQ .= 'WHERE `SQId` = ' . $intSQId . ';';
        $rs_editSQ = $this->mysqli->query($sql_editSQ);
        if (!$rs_editSQ) {
            throw new Exception('更新子題資料時發生錯誤！');
        }else {
            $bolRtn = true;
        }
        return $bolRtn;
    }

    // $intSQId: int
    // Returns Boolean
    public function delSubQuestionChkUserId($intSQId,$intUserId) {
        $bolRtn = false;
        //檢查子題編號是否為空白
        if (empty ($intSQId)) {
            throw new Exception('錯誤！缺少子題編號。');
        }
        
        if (empty($intUserId)) {
            throw new Exception('缺少使用者編號。');
        }
        
        //核對刪除者否為建立者
        $arySQInfo = $this->getSubQuestionInfoBySQId($intSQId);
        if($arySQInfo[5] != $intUserId){
            throw new Exception('禁止刪除其他使用者建立的子題。');
        }
        
        //刪除
        $sql_delSQ = 'DELETE FROM `SubQuestion` WHERE `SQId`=' . $intSQId;
        $rs_delSQ = $this->mysqli->query($sql_delSQ);
        if (!$rs_delSQ) {
            throw new Exception('錯誤！刪除子題時發生錯誤。');
        }else {
            $bolRtn = true;
        }

        return $bolRtn;

    }

    // $intSQId: int
    // Returns Array
    public function getSubQuestionInfoBySQId($intSQId) {
        //檢查子題編號是否為空白
        if (empty ($intSQId)) {
            throw new Exception('缺少子題編號！');
        }

        $sql_getSQById = 'SELECT * FROM `SubQuestion` WHERE `SQId`=' . $intSQId;
        $rs_getSQById = $this->mysqli->query($sql_getSQById);

        if (!$rs_getSQById) {
            throw new Exception('無法查詢該子題的資料！');
        }else {
            if ($rs_getSQById->num_rows == 0) {
                throw new Exception('無此子題編號的資料！');
            }else {
                $row_getSQById = $rs_getSQById->fetch_row();
                return $row_getSQById;
            }
        } //End If
    }

    // Returns Array
    public function getSubQuestionListByMQId($intMQId) {
        $sql_getSQList = 'SELECT * FROM `SubQuestion` WHERE `MQId`=' . $intMQId . ' ORDER BY `SQId`';
        $rs_getSQList = $this->mysqli->query($sql_getSQList);

        if (!$rs_getSQList) {
            throw new Exception('無法查詢該子題的資料！');
        }else {
            $arySQList = array();
            while ($aryOneSQ = $rs_getSQList->fetch_row()) {
                array_push($arySQList, $aryOneSQ);
            }
            return $arySQList;
        } //End If
    }

    private function getNowTime() {
        return date('Y-m-d H:i:s');
    }

    function  __destruct() {
        if ($this->mysqli) {
            $this->mysqli->close();
            $this->mysqli = FALSE;
        }
    }
}
?>
